package cn.zhouyafeng.itchat4j.controller;

import cn.zhouyafeng.itchat4j.MultiWechat;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.core.MultiCore;
import cn.zhouyafeng.itchat4j.service.ILoginService;
import cn.zhouyafeng.itchat4j.service.impl.MultiCoreLoginServiceImpl;
import cn.zhouyafeng.itchat4j.thread.MultiCheckLoginStatusThread;
import cn.zhouyafeng.itchat4j.utils.SleepUtils;
import cn.zhouyafeng.itchat4j.utils.tools.CommonTools;

import java.util.concurrent.Future;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: ZhaoTao
 * \* Date: 2017/12/11
 * \* Time: 10:03
 * \* Description:
 * \
 */
public class MultiCoreLoginController {

    private UserLogger LOG =  UserLogger.getUserLogger();
    private ILoginService loginService = null;
    private MultiCore core;
    private String key;


    public MultiCoreLoginController(String key) {
        this.key = key;
        MultiCore.remove(key);
        core = MultiCore.getInstance(key);
        loginService = new MultiCoreLoginServiceImpl(key);
    }

    public MultiCore getCore() {
        return core;
    }

    /**
     * 获取二维码链接
     * 重新获取
     * @return
     */
    public String getQrUrl() {
        LOG.info("1. 获取微信UUID");
        for (int count = 0; count < 10; count++) {
            while (loginService.getUuid() == null) {
                while (loginService.getUuid() == null) {
                    LOG.warn("1.1. 获取微信UUID失败，两秒后重新获取");
                    SleepUtils.sleep(2000);
                }
            }
        }
        LOG.info("2. 获取登陆二维码图片");
        return loginService.getQrUrl();
    }

    /**
     * 登陆
     */
    public boolean login() {
        int count = 0;
        while (true) {
            if (!core.isAlive()) {
                loginService.login();
                core.setAlive(true);
                LOG.info(("登陆成功"));
                return true;
            }
            count++;
            SleepUtils.sleep(1000);
            if (count > 30) {
                LOG.info("4. 登陆超时，请重新扫描二维码图片");
                return false;
            }
        }
    }

    /**
     * 初始化
     */
    public void init() {
        if (!loginService.webWxInit()) {
            LOG.info("6. 微信初始化异常");
            System.exit(0);
        }
        LOG.info("6. 开启微信状态通知");
        loginService.wxStatusNotify();
        LOG.info("7. 清除。。。。");
        CommonTools.clearScreen();
        LOG.info(String.format("欢迎回来， %s", core.getNickName()));
        LOG.info("9. 获取联系人信息");
        loginService.webWxGetContact();
        LOG.info("11. 缓存本次登陆好友相关消息");
        WechatTools.setUserInfo(core); // 登陆成功后缓存本次登陆好友相关消息（NickName, UserName）
        //Future<Boolean> f = MultiWechat.LoginPool.submit(new MultiCheckLoginStatusThread(key)); //在线程池中监测
        //loginService.qunfa("11", false); //群发
    }

    /**
     * 群发操作
     * @param msg
     * @param qf
     */
    public void qunfa(String msg,boolean qf) {
        loginService.qunfa(msg, qf);
    }

    public void login(String qrPath) {
        if (core.isAlive()) { // 已登陆
            LOG.info("itchat4j已登陆");
            return;
        }
        while (true) {
            for (int count = 0; count < 10; count++) {
                LOG.info("获取UUID");
                while (loginService.getUuid() == null) {
                    LOG.info("1. 获取微信UUID");
                    while (loginService.getUuid() == null) {
                        LOG.warn("1.1. 获取微信UUID失败，两秒后重新获取");
                        SleepUtils.sleep(2000);
                    }
                }
                LOG.info("2. 获取登陆二维码图片");
                if (loginService.getQR(qrPath)) {
                    break;
                } else if (count == 10) {
                    LOG.error("2.2. 获取登陆二维码图片失败，系统退出");
                    System.exit(0);
                }
            }
            LOG.info("3. 请扫描二维码图片，并在手机上确认");
            if (!core.isAlive()) {
                loginService.login();
                core.setAlive(true);
                LOG.info(("登陆成功"));
                break;
            }
            LOG.info("4. 登陆超时，请重新扫描二维码图片");
        }

        LOG.info("5. 登陆成功，微信初始化");
        if (!loginService.webWxInit()) {
            LOG.info("6. 微信初始化异常");
            System.exit(0);
        }

        LOG.info("6. 开启微信状态通知");
        loginService.wxStatusNotify();

        LOG.info("7. 清除。。。。");
        CommonTools.clearScreen();
        LOG.info(String.format("欢迎回来， %s", core.getNickName()));

        LOG.info("8. 开始接收消息");
        loginService.startReceiving();

        LOG.info("9. 获取联系人信息");
        loginService.webWxGetContact();

        LOG.info("10. 获取群好友及群好友列表");
        loginService.WebWxBatchGetContact();

        LOG.info("11. 缓存本次登陆好友相关消息");
        WechatTools.setUserInfo(core); // 登陆成功后缓存本次登陆好友相关消息（NickName, UserName）

        LOG.info("12.开启微信状态检测线程");

        Future<Boolean> f = MultiWechat.LoginPool.submit(new MultiCheckLoginStatusThread(key)); //在线程池中监测

        loginService.qunfa("11", false);
    }
}
